package com.dqp.cslggroup.Util;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;

import androidx.annotation.Nullable;
import com.dqp.cslggroup.UI.MyApplication;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.prefs.PreferenceChangeEvent;

public class SharedPreferencesUtil {

    public static void removeSp(Context context, String name) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        editor.remove(name);
        editor.apply();
    }

    public static String getroomverifySp(Context context) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        String rel = sp.getString("roomverify", null);
        editor.apply();
        return rel;
    }

    public static String getStringSp(Context context, String name, String defValue) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        String rel = sp.getString(name, defValue);
        editor.apply();
        return rel;
    }

    public static String getIp(Context context) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        String rel = sp.getString("ip", null);
        editor.apply();
        return rel;
    }


    public static void putStringSp(Context context, String name, String result) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();

        editor.putString(name, result);
        editor.apply();

    }

    public static boolean getBooleanSp(Context context, String name, boolean defValue) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        boolean rel = sp.getBoolean(name, defValue);
        editor.apply();
        return rel;
    }

    public static void putBooleanSp(Context context, String name, boolean result) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        editor.putBoolean(name, result);
        editor.apply();
    }

    public static int getIntSp(Context context, String name, int defValue) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        int rel = sp.getInt(name, defValue);
        editor.apply();
        return rel;
    }

    public static void putIntSp(Context context, String name, int result) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        editor.putInt(name, result);
        editor.apply();
    }

    public static long getLongSp(Context context, String name, long defValue) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        long rel = sp.getLong(name, defValue);
        editor.apply();
        return rel;
    }

    public static void putLongSp(Context context, String name, long result) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        editor.putLong(name, result);
        editor.apply();
    }


    public static void setMap(Context context, String key, Map<String, String> datas) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        JSONArray mJsonArray = new JSONArray();
        Iterator<Map.Entry<String, String>> iterator = datas.entrySet().iterator();
        JSONObject object = new JSONObject();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            try {
                object.put(entry.getKey(), entry.getValue());
            } catch (JSONException ignored) {
            }
        }
        mJsonArray.put(object);
        editor.putString(key, mJsonArray.toString());
        editor.apply();
    }

    public static Map<String, String> getMap(Context context,String key) {
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        Map<String, String> datas = new HashMap<>();
        String result = sp.getString(key, "");
        try {
            JSONArray array = new JSONArray(result);
            for (int i = 0; i < array.length(); i++) {
                JSONObject itemObject = array.getJSONObject(i);
                JSONArray names = itemObject.names();
                if (names != null) {
                    for (int j = 0; j < names.length(); j++) {
                        String name = names.getString(j);
                        String value = itemObject.getString(name);
                        datas.put(name, value);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return datas;
    }

}
